﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace YhmOA.Common
{
    //OA菜单权限XML解析类
    public class MenuConfig
    {
        /// <summary>
        /// 获取顶部菜单
        /// </summary>
        /// <returns></returns>
        public static List<string> GetTopMenu()
        {
            return GetTopMenuXML();
        }

        /// <summary>
        /// 获取左边菜单
        /// </summary>
        /// <param name="name">顶部菜单的名称</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetLeftMenu(string name)
        {
            return GetLeftMenuXML()[name];
        }

        private static Dictionary<string, Dictionary<string, object>> GetLeftMenuXML()
        {
            if (DataCache.GetCache("____MenuConfig____left") == null)
                DataCache.SetCache("____MenuConfig____left", AnalysisXMLLeft());
            return DataCache.GetCache("____MenuConfig____left") as Dictionary<string, Dictionary<string, object>>;
        }

        private static List<string> GetTopMenuXML()
        {
            if (DataCache.GetCache("____MenuConfig____top") == null)
                DataCache.SetCache("____MenuConfig____top", AnalysisXMLTop());
            return DataCache.GetCache("____MenuConfig____top") as List<string>;
        }

        /// <summary>
        /// 获取xml的路径
        /// </summary>
        private static string XmlPath
        {
            get
            {
                string file = HttpContext.Current.Server.MapPath("~") + "Config\\Menu.xml";
                return file;
            }
        }

        #region 解析xml
        /// <summary>
        /// 解析xml
        /// </summary>
        private static Dictionary<string, Dictionary<string, object>> AnalysisXMLLeft()
        {
            Dictionary<string, Dictionary<string, object>> dicTopMenu = new Dictionary<string, Dictionary<string, object>>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(XmlPath);
            //获取所有的顶部菜单
            XmlNodeList topMenuList = xmlDoc.SelectNodes("menu/topMenu");
            //便利顶部菜单
            foreach (XmlNode topMenuItem in topMenuList)
            {
                //获取当前顶部菜单
                string topMenu = (topMenuItem as XmlElement).GetAttribute("text");

                Dictionary<string, object> dicTopLeft = new Dictionary<string, object>();
                //获取所有的右边菜单的顶级菜单
                XmlNodeList topLeftList = topMenuItem.SelectNodes("topLeft");
                //便利左边菜单的顶级菜单
                foreach (XmlNode topLeftItem in topLeftList)
                {
                    #region 
                    //获取左边菜单的顶级菜单的名称
                    string topLeft = (topLeftItem as XmlElement).GetAttribute("text");
                    //获取左边菜单的二级菜单（顶级）
                    XmlNodeList topList = topLeftItem.SelectNodes("top");
                    //当二级菜单个数大于0时
                    if (topList.Count > 0)
                    {
                        Dictionary<string, List<MenuInfo>> dicTop = new Dictionary<string, List<MenuInfo>>();
                        //便利二级菜单
                        foreach (XmlNode topItem in topList)
                        {
                            //获取二级菜单的名称
                            string top = (topItem as XmlElement).GetAttribute("text");
                            //获取二级菜单下的内容菜单
                            XmlNodeList leftList = topItem.SelectNodes("left");
                            //定义存放内容菜单的集合
                            List<MenuInfo> listLeft = new List<MenuInfo>();
                            //便利当前二级菜单下的所有内容菜单
                            foreach (XmlNode leftItem in leftList)
                            {
                                listLeft.Add(new MenuInfo((leftItem as XmlElement).GetAttribute("text"), (leftItem as XmlElement).GetAttribute("url"), (leftItem as XmlElement).GetAttribute("img"), (leftItem as XmlElement).GetAttribute("type")));
                            }
                            dicTop.Add(top, listLeft);
                        }
                        dicTopLeft.Add(topLeft, dicTop);
                    }
                    else//当二级菜单不存在的时候，获取内容菜单
                    {
                        //定义存放内容菜单的集合
                        List<MenuInfo> listLeft = new List<MenuInfo>();
                        //获取当前菜单下的所有内容菜单
                        XmlNodeList leftList = topLeftItem.SelectNodes("left");
                        //便利内容菜单
                        foreach (XmlNode leftItem in leftList)
                        {
                            listLeft.Add(new MenuInfo((leftItem as XmlElement).GetAttribute("text"), (leftItem as XmlElement).GetAttribute("url"), (leftItem as XmlElement).GetAttribute("img"), (leftItem as XmlElement).GetAttribute("type")));
                        }
                        dicTopLeft.Add(topLeft, listLeft);
                    }
                    #endregion
                }
                dicTopMenu.Add(topMenu, dicTopLeft);
            }
            return dicTopMenu; ;
        }
        #endregion

        #region 解析xml
        /// <summary>
        /// 解析xml
        /// </summary>
        private static List<string> AnalysisXMLTop()
        {
            List<string> list = new List<string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(XmlPath);
            XmlNodeList xmlList = xmlDoc.SelectNodes("menu/topMenu");
            foreach (XmlNode item in xmlList)
            {
                list.Add((item as XmlElement).GetAttribute("text"));
            }
            return list;
        }
        #endregion
    }

    [Serializable]
    public class MenuInfo
    {
        private string text;
        private string url;
        private string img;
        private string type;
        /// <summary>
        /// 菜单对应的url的呈现方式
        /// </summary>
        public string Type
        {
            get { return type; }
            set { type = value; }
        }
        /// <summary>
        /// 菜单对应的图片
        /// </summary>
        public string Img
        {
            get { return img; }
            set { img = value; }
        }
        /// <summary>
        /// 菜单对应的url
        /// </summary>
        public string Url
        {
            get { return url; }
            set { url = value; }
        }
        /// <summary>
        /// 菜单的文本
        /// </summary>
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="text">菜单的文本</param>
        /// <param name="url">菜单对应的url</param>
        /// <param name="img">菜单对应的图片</param>
        /// <param name="type">菜单对应的url的呈现方式</param>
        public MenuInfo(string text, string url, string img, string type)
        {
            this.text = text;
            this.url = url;
            this.img = img;
            this.type = type;
        }
    }
}
